package com.xoms.front.constants;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 应用配置
 */
public class AppConfig {
	private static Logger logger = Logger.getLogger(AppConfig.class);

	private String SERVER_IP = "";
	private String SERVER_PORT = "";
	private String SERVER_DOMAIN = "";
	private String SERVER_APP_PATH = "";

	private static final String appConfigFile = "/appconfig.properties";
	private static AppConfig config = new AppConfig();

	public AppConfig() {
		Properties properties = new Properties();
		InputStream inStream = this.getClass().getResourceAsStream(appConfigFile);
		if (inStream == null) {
			logger.error("根目录下找不到appconfig.properties文件");
			return;
		}
		try {
			properties.load(inStream);
			this.SERVER_IP = properties.getProperty("server.ip");
			this.SERVER_PORT = properties.getProperty("server.port");
			this.SERVER_DOMAIN = properties.getProperty("server.domain");
			this.SERVER_APP_PATH = properties.getProperty("server.app.path");
		} catch (IOException e) {
			logger.error("load appconfig.properties error,class 根目录下找不到文件appconfig.properties文件");
			e.printStackTrace();
		}
		logger.info("load appconfig.properties success");
	}

	public static AppConfig instance() {
		synchronized (AppConfig.class) {
			if (config == null)
				config = new AppConfig();
			return config;
		}
	}

	public static void main(String[] args) {
		System.out.println(AppConfig.instance().getWebAppPath());
	}

	public String getWebAppPath() {
		String path = "";
		if (SERVER_IP != null && SERVER_IP != "") {
			path = "http://" + SERVER_IP + ":" + SERVER_PORT;
			if (SERVER_APP_PATH != null && SERVER_APP_PATH != "") {
				path += "/" + SERVER_APP_PATH;
			}
		}
		if (SERVER_DOMAIN != null && SERVER_DOMAIN != "") {
			path = "http://" + SERVER_IP + ":" + SERVER_PORT;
			if (SERVER_APP_PATH != null && SERVER_APP_PATH != "") {
				path += "/" + SERVER_APP_PATH;
			}
		}
		return path;
	}

	public String getSERVER_IP() {
		return SERVER_IP;
	}

	public void setSERVER_IP(String sERVER_IP) {
		SERVER_IP = sERVER_IP;
	}

	public String getSERVER_PORT() {
		return SERVER_PORT;
	}

	public void setSERVER_PORT(String sERVER_PORT) {
		SERVER_PORT = sERVER_PORT;
	}

	public String getSERVER_DOMAIN() {
		return SERVER_DOMAIN;
	}

	public void setSERVER_DOMAIN(String sERVER_DOMAIN) {
		SERVER_DOMAIN = sERVER_DOMAIN;
	}

	public String getSERVER_APP_PATH() {
		return SERVER_APP_PATH;
	}

	public void setSERVER_APP_PATH(String sERVER_APP_PATH) {
		SERVER_APP_PATH = sERVER_APP_PATH;
	}

}
